package com.yingjia.yingjiauniversity.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;

import io.reactivex.disposables.CompositeDisposable;

/**
 * Created by binbin.liu.o on 2017/12/27.
 */

public class BaseFragment extends Fragment implements MvpView {

    private BaseActivity mActivity;
    protected CompositeDisposable compositeDisposable;

    private BroadcastReceiver mConnectReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(final Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                boolean isDisconnected = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                if (isDisconnected) {
                    showNetworkDisconnectView();
                } else {
                    hideNetworkDisconnectView();
                }
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter connectFilter = new IntentFilter();
        connectFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        getBaseActivity().registerReceiver(mConnectReceiver, connectFilter);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mConnectReceiver != null) {
            getBaseActivity().unregisterReceiver(mConnectReceiver);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        compositeDisposable = new CompositeDisposable();
        if (context instanceof BaseActivity) {
            BaseActivity activity = (BaseActivity) context;
            this.mActivity = activity;
            activity.onFragmentAttached();
        }
    }

    @Override
    public void onDetach() {
        mActivity = null;
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void showLoading(boolean cancelable, Object tag) {
        if (mActivity != null) {
            mActivity.showLoading(cancelable, tag);
        }
    }

    @Override
    public void hideLoading() {
        if (mActivity != null) {
            mActivity.hideLoading();
        }
    }

    @Override
    public void showToast(String message) {
        if (mActivity != null) {
            mActivity.showToast(message);
        }
    }

    @Override
    public void showToast(@StringRes int resId) {
        if (mActivity != null) {
            mActivity.showToast(resId);
        }
    }

    public BaseActivity getBaseActivity() {
        return mActivity;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mConnectReceiver = null;
        compositeDisposable.clear();
    }

    /**
     * 网络断开
     */
    public void showNetworkDisconnectView() {
    }

    /**
     * 网络连接
     */
    public void hideNetworkDisconnectView() {
    }

    public interface Callback {

        void onFragmentAttached();

        void onFragmentDetached(String tag);
    }
}
